package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.SecurityUtils;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.domain.SysMovement;
import com.zhuoyue.system.domain.dto.SysMovementDTO;
import com.zhuoyue.system.service.FileUploadService;
import com.zhuoyue.system.service.ISysMovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业动态Controller
 *
 * @author zhanghui
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/system/movement")
public class SysMovementController extends BaseController {
    @Autowired
    private ISysMovementService sysMovementService;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 查询企业动态列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMovement sysMovement) {
        startPage();
        List<SysMovement> list = sysMovementService.selectSysMovementList(sysMovement);
        return getDataTable(list);
    }

    /**
     * 查询企业动态列表(api)
     */
    @GetMapping("/listForApi")
    public TableDataInfo listForApi(String orgCode) {
        List<SysMovementDTO> list = sysMovementService.selectSysMovementListForApi(orgCode);
        return getDataTable(list);
    }

    /**
     * 导出企业动态列表
     */
    @Log(title = "企业动态", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysMovement sysMovement) {
        List<SysMovement> list = sysMovementService.selectSysMovementList(sysMovement);
        ExcelUtil<SysMovement> util = new ExcelUtil<SysMovement>(SysMovement.class);
        return util.exportExcel(list, "动态");
    }

    /**
     * 新增企业动态
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysMovement sysMovement) {
        // 若修改为发布状态, 则将同组织下同类型的其他动态修改为未发布
        if (sysMovement.getStatus() != null && "0".equals(sysMovement.getStatus())) {
            sysMovementService.setStatusDisableOfOrgAndType(sysMovement.getDeptId(), sysMovement.getDictCode());
        }

        sysMovement.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysMovementService.insertSysMovement(sysMovement));
    }

    /**
     * 修改企业动态
     */
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody SysMovement sysMovement) {
        // 若修改为发布状态, 则将同组织下同类型的其他动态修改为未发布
        if (sysMovement.getStatus() != null && "0".equals(sysMovement.getStatus())) {
            sysMovementService.setStatusDisableOfOrgAndType(sysMovement.getDeptId(), sysMovement.getDictCode());
        }

        SysMovement preSysMovement = sysMovementService.selectSysMovementById(sysMovement.getId());
        int index = -1;
        // 若当前icon为null, 或当前不为null且先前也不为bull, 则判断是否相等
        String nowIconUrl = sysMovement.getIconUrl();
        String preIconUrl = preSysMovement.getIconUrl();
        if (preIconUrl != null) {
            if (nowIconUrl == null || !nowIconUrl.equals(preIconUrl)) {
                index = preIconUrl.indexOf("/zyzn");
                if (index > 0) {
                    fileUploadService.delete(preIconUrl.substring(index + 1));
                }
            }
        }

        sysMovement.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysMovementService.updateSysMovement(sysMovement));
    }

    /**
     * 修改企业动态发布状态
     */
    @PreAuthorize("@ss.hasPermi('content:movement:release')")
    @PutMapping("/updateStatus")
    public AjaxResult editStatus(@RequestBody SysMovement sysMovement) {
        // 若修改为发布状态, 则将同组织下同类型的其他动态修改为未发布
        if (sysMovement.getStatus() != null && "0".equals(sysMovement.getStatus())) {
            sysMovementService.setStatusDisableOfOrgAndType(sysMovement.getDeptId(), sysMovement.getDictCode());
        }

        SysMovement preSysMovement = sysMovementService.selectSysMovementById(sysMovement.getId());
        int index = -1;
        // 若当前icon为null, 或当前不为null且先前也不为bull, 则判断是否相等
        String nowIconUrl = sysMovement.getIconUrl();
        String preIconUrl = preSysMovement.getIconUrl();
        if (preIconUrl != null) {
            if (nowIconUrl == null || !nowIconUrl.equals(preIconUrl)) {
                index = preIconUrl.indexOf("/zyzn");
                if (index > 0) {
                    fileUploadService.delete(preIconUrl.substring(index + 1));
                }
            }
        }

        sysMovement.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysMovementService.updateSysMovement(sysMovement));
    }

    /**
     * 删除企业动态
     */
    @DeleteMapping("/delete")
    public AjaxResult remove(Long id) {
        SysMovement sysMovement = sysMovementService.selectSysMovementById(id);
        int index = -1;

        // 删除图标
        if (sysMovement.getIconUrl() != null) {
            index = sysMovement.getIconUrl().indexOf("/zyzn");
            if (index > 0) {
                fileUploadService.delete(sysMovement.getIconUrl().substring(index + 1));
            }
        }

        return toAjax(sysMovementService.deleteSysMovementById(id));
    }

    @PutMapping("/addClickCount")
    public void getMessAge(Long id) {
        sysMovementService.addClickCount(id, 1);
    }
}
